﻿using System;
using System.Data;
using System.Text;
using MySql.Data.MySqlClient;
using DB.DBUtility;//Please add references
namespace DB.DAL
{
    /// <summary>
    /// 数据访问类:Axle
    /// </summary>
    public partial class Axle
    {
        public Axle()
        { }
        #region  BasicMethod
        /// <summary>
        /// 是否存在该记录
        /// </summary>
        public bool Exists(int ID)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) from Axle");
            strSql.Append(" where ID=@ID");
            MySqlParameter[] parameters = {
					new MySqlParameter("@ID", MySqlDbType.Int32)
			};
            parameters[0].Value = ID;

            return DbHelperMySQL.Exists(strSql.ToString(), parameters);
        }


        /// <summary>
        /// 增加一条数据
        /// </summary>
        public bool Add(DB.Model.Axle model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("insert into Axle(");
            strSql.Append("Name,PowerShiftStation,RunningUnit,RelativeZero,LeadDistance,DriveRatio,AxleDetailSetting,Radian,RunSpeed,TozeroSpeed,Position,OneStepAddtion,RunTarget,RunAcceleration)");
            strSql.Append(" values (");
            strSql.Append("@Name,@PowerShiftStation,@RunningUnit,@RelativeZero,@LeadDistance,@DriveRatio,@AxleDetailSetting,@Radian,@RunSpeed,@TozeroSpeed,@Position,@OneStepAddtion,@RunTarget,@RunAcceleration)");
            MySqlParameter[] parameters = {
					new MySqlParameter("@Name", MySqlDbType.VarChar,255),
					new MySqlParameter("@PowerShiftStation", MySqlDbType.Int32,11),
					new MySqlParameter("@RunningUnit", MySqlDbType.Int32,11),
					new MySqlParameter("@RelativeZero", MySqlDbType.Double),
					new MySqlParameter("@LeadDistance", MySqlDbType.Double),
					new MySqlParameter("@DriveRatio", MySqlDbType.Double),
					new MySqlParameter("@AxleDetailSetting", MySqlDbType.Int32,11),
					new MySqlParameter("@Radian", MySqlDbType.Double),
					new MySqlParameter("@RunSpeed", MySqlDbType.Double),
					new MySqlParameter("@TozeroSpeed", MySqlDbType.Double),
					new MySqlParameter("@Position", MySqlDbType.Double),
					new MySqlParameter("@OneStepAddtion", MySqlDbType.Int32,11),
					new MySqlParameter("@RunTarget", MySqlDbType.Double),
					new MySqlParameter("@RunAcceleration", MySqlDbType.Int32,11)};
            parameters[0].Value = model.Name;
            parameters[1].Value = model.PowerShiftStation;
            parameters[2].Value = model.RunningUnit;
            parameters[3].Value = model.RelativeZero;
            parameters[4].Value = model.LeadDistance;
            parameters[5].Value = model.DriveRatio;
            parameters[6].Value = model.AxleDetailSetting;
            parameters[7].Value = model.Radian;
            parameters[8].Value = model.RunSpeed;
            parameters[9].Value = model.TozeroSpeed;
            parameters[10].Value = model.Position;
            parameters[11].Value = model.OneStepAddtion;
            parameters[12].Value = model.RunTarget;
            parameters[13].Value = model.RunAcceleration;

            int rows = DbHelperMySQL.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
        /// <summary>
        /// 更新一条数据
        /// </summary>
        public bool Update(DB.Model.Axle model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update Axle set ");
            strSql.Append("Name=@Name,");
            strSql.Append("PowerShiftStation=@PowerShiftStation,");
            strSql.Append("RunningUnit=@RunningUnit,");
            strSql.Append("RelativeZero=@RelativeZero,");
            strSql.Append("LeadDistance=@LeadDistance,");
            strSql.Append("DriveRatio=@DriveRatio,");
            strSql.Append("AxleDetailSetting=@AxleDetailSetting,");
            strSql.Append("Radian=@Radian,");
            strSql.Append("RunSpeed=@RunSpeed,");
            strSql.Append("TozeroSpeed=@TozeroSpeed,");
            strSql.Append("Position=@Position,");
            strSql.Append("OneStepAddtion=@OneStepAddtion,");
            strSql.Append("RunTarget=@RunTarget,");
            strSql.Append("RunAcceleration=@RunAcceleration");
            strSql.Append(" where ID=@ID");
            MySqlParameter[] parameters = {
					new MySqlParameter("@Name", MySqlDbType.VarChar,255),
					new MySqlParameter("@PowerShiftStation", MySqlDbType.Int32,11),
					new MySqlParameter("@RunningUnit", MySqlDbType.Int32,11),
					new MySqlParameter("@RelativeZero", MySqlDbType.Double),
					new MySqlParameter("@LeadDistance", MySqlDbType.Double),
					new MySqlParameter("@DriveRatio", MySqlDbType.Double),
					new MySqlParameter("@AxleDetailSetting", MySqlDbType.Int32,11),
					new MySqlParameter("@Radian", MySqlDbType.Double),
					new MySqlParameter("@RunSpeed", MySqlDbType.Double),
					new MySqlParameter("@TozeroSpeed", MySqlDbType.Double),
					new MySqlParameter("@Position", MySqlDbType.Double),
					new MySqlParameter("@OneStepAddtion", MySqlDbType.Int32,11),
					new MySqlParameter("@RunTarget", MySqlDbType.Double),
					new MySqlParameter("@RunAcceleration", MySqlDbType.Int32,11),
					new MySqlParameter("@ID", MySqlDbType.Int32,11)};
            parameters[0].Value = model.Name;
            parameters[1].Value = model.PowerShiftStation;
            parameters[2].Value = model.RunningUnit;
            parameters[3].Value = model.RelativeZero;
            parameters[4].Value = model.LeadDistance;
            parameters[5].Value = model.DriveRatio;
            parameters[6].Value = model.AxleDetailSetting;
            parameters[7].Value = model.Radian;
            parameters[8].Value = model.RunSpeed;
            parameters[9].Value = model.TozeroSpeed;
            parameters[10].Value = model.Position;
            parameters[11].Value = model.OneStepAddtion;
            parameters[12].Value = model.RunTarget;
            parameters[13].Value = model.RunAcceleration;
            parameters[14].Value = model.ID;

            int rows = DbHelperMySQL.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// 删除一条数据
        /// </summary>
        public bool Delete(int ID)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from Axle ");
            strSql.Append(" where ID=@ID");
            MySqlParameter[] parameters = {
					new MySqlParameter("@ID", MySqlDbType.Int32)
			};
            parameters[0].Value = ID;

            int rows = DbHelperMySQL.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
        /// <summary>
        /// 批量删除数据
        /// </summary>
        public bool DeleteList(string IDlist)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from Axle ");
            strSql.Append(" where ID in (" + IDlist + ")  ");
            int rows = DbHelperMySQL.ExecuteSql(strSql.ToString());
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }


        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public DB.Model.Axle GetModel(int ID)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("select ID,Name,PowerShiftStation,RunningUnit,RelativeZero,LeadDistance,DriveRatio,AxleDetailSetting,Radian,RunSpeed,TozeroSpeed,Position,OneStepAddtion,RunTarget,RunAcceleration from Axle ");
            strSql.Append(" where ID=@ID");
            MySqlParameter[] parameters = {
					new MySqlParameter("@ID", MySqlDbType.Int32)
			};
            parameters[0].Value = ID;

            DB.Model.Axle model = new DB.Model.Axle();
            DataSet ds = DbHelperMySQL.Query(strSql.ToString(), parameters);
            if (ds.Tables[0].Rows.Count > 0)
            {
                return DataRowToModel(ds.Tables[0].Rows[0]);
            }
            else
            {
                return null;
            }
        }


        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public DB.Model.Axle DataRowToModel(DataRow row)
        {
            DB.Model.Axle model = new DB.Model.Axle();
            if (row != null)
            {
                if (row["ID"] != null && row["ID"].ToString() != "")
                {
                    model.ID = int.Parse(row["ID"].ToString());
                }
                if (row["Name"] != null)
                {
                    model.Name = row["Name"].ToString();
                }
                if (row["PowerShiftStation"] != null && row["PowerShiftStation"].ToString() != "")
                {
                    model.PowerShiftStation = int.Parse(row["PowerShiftStation"].ToString());
                }
                if (row["RunningUnit"] != null && row["RunningUnit"].ToString() != "")
                {
                    model.RunningUnit = int.Parse(row["RunningUnit"].ToString());
                }
                if (row["RelativeZero"] != null && row["RelativeZero"].ToString() != "")
                {
                    model.RelativeZero = double.Parse( row["RelativeZero"].ToString());
                }
                if (row["LeadDistance"] != null && row["LeadDistance"].ToString() != "")
                {
                    model.LeadDistance = double.Parse(row["LeadDistance"].ToString());
                }
                if (row["DriveRatio"] != null && row["DriveRatio"].ToString() != "")
                {
                    model.DriveRatio = double.Parse(row["DriveRatio"].ToString());
                }
                if (row["AxleDetailSetting"] != null && row["AxleDetailSetting"].ToString() != "")
                {
                    model.AxleDetailSetting = int.Parse(row["AxleDetailSetting"].ToString());
                }
                if (row["Radian"] != null && row["Radian"].ToString() != "")
                {
                model.Radian = double.Parse( row["Radian"].ToString());
                }
                if (row["RunSpeed"] != null && row["RunSpeed"].ToString() != "")
                {
                    model.RunSpeed = double.Parse( row["RunSpeed"].ToString() );
                }
                if (row["TozeroSpeed"] != null && row["TozeroSpeed"].ToString() != "")
                {
                    model.TozeroSpeed = double.Parse( row["TozeroSpeed"].ToString() );
                }
                if (row["Position"] != null && row["Position"].ToString() != "")
                {
                    model.Position = double.Parse( row["Position"].ToString());
                }
                if (row["OneStepAddtion"] != null && row["OneStepAddtion"].ToString() != "")
                {
                    model.OneStepAddtion = int.Parse(row["OneStepAddtion"].ToString());
                }
                if (row["RunTarget"] != null && row["RunTarget"].ToString() != "")
                {
                    model.RunTarget = double.Parse(row["RunTarget"].ToString());
                }
                if (row["RunAcceleration"] != null && row["RunAcceleration"].ToString() != "")
                {
                    model.RunAcceleration = int.Parse(row["RunAcceleration"].ToString());
                }
            }
            return model;
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetList(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select ID,Name,PowerShiftStation,RunningUnit,RelativeZero,LeadDistance,DriveRatio,AxleDetailSetting,Radian,RunSpeed,TozeroSpeed,Position,OneStepAddtion,RunTarget,RunAcceleration ");
            strSql.Append(" FROM Axle ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            return DbHelperMySQL.Query(strSql.ToString());
        }

        /// <summary>
        /// 获取记录总数
        /// </summary>
        public int GetRecordCount(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) FROM Axle ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            object obj = DbHelperSQL.GetSingle(strSql.ToString());
            if (obj == null)
            {
                return 0;
            }
            else
            {
                return Convert.ToInt32(obj);
            }
        }
        /// <summary>
        /// 分页获取数据列表
        /// </summary>
        public DataSet GetListByPage(string strWhere, string orderby, int startIndex, int endIndex)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("SELECT * FROM ( ");
            strSql.Append(" SELECT ROW_NUMBER() OVER (");
            if (!string.IsNullOrEmpty(orderby.Trim()))
            {
                strSql.Append("order by T." + orderby);
            }
            else
            {
                strSql.Append("order by T.ID desc");
            }
            strSql.Append(")AS Row, T.*  from Axle T ");
            if (!string.IsNullOrEmpty(strWhere.Trim()))
            {
                strSql.Append(" WHERE " + strWhere);
            }
            strSql.Append(" ) TT");
            strSql.AppendFormat(" WHERE TT.Row between {0} and {1}", startIndex, endIndex);
            return DbHelperMySQL.Query(strSql.ToString());
        }

        /*
        /// <summary>
        /// 分页获取数据列表
        /// </summary>
        public DataSet GetList(int PageSize,int PageIndex,string strWhere)
        {
            MySqlParameter[] parameters = {
                    new MySqlParameter("@tblName", MySqlDbType.VarChar, 255),
                    new MySqlParameter("@fldName", MySqlDbType.VarChar, 255),
                    new MySqlParameter("@PageSize", MySqlDbType.Int32),
                    new MySqlParameter("@PageIndex", MySqlDbType.Int32),
                    new MySqlParameter("@IsReCount", MySqlDbType.Bit),
                    new MySqlParameter("@OrderType", MySqlDbType.Bit),
                    new MySqlParameter("@strWhere", MySqlDbType.VarChar,1000),
                    };
            parameters[0].Value = "Axle";
            parameters[1].Value = "ID";
            parameters[2].Value = PageSize;
            parameters[3].Value = PageIndex;
            parameters[4].Value = 0;
            parameters[5].Value = 0;
            parameters[6].Value = strWhere;	
            return DbHelperMySQL.RunProcedure("UP_GetRecordByPage",parameters,"ds");
        }*/

        #endregion  BasicMethod
        #region  ExtensionMethod

        #endregion  ExtensionMethod
    }
}



